
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_common/util/string_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../flutter_common.dart';

//fontSize30 = 15sp
final double fontSize60 = 60.sp;
final double fontSize42 = 42.sp;
final double fontSize40 = 40.sp;
final double fontSize36 = ScreenUtil().setSp(36);
final double fontSize34 = ScreenUtil().setSp(34);
final double fontSize32 = ScreenUtil().setSp(32);
final double fontSize30 = ScreenUtil().setSp(30);
final double fontSize28 = ScreenUtil().setSp(28);
final double fontSize26 = ScreenUtil().setSp(26);
final double fontSize24 = ScreenUtil().setSp(24);
final double fontSize22 = ScreenUtil().setSp(22);
final double fontSize20 = ScreenUtil().setSp(20);
final double fontSize18 = ScreenUtil().setSp(18);
final double fontSize16 = ScreenUtil().setSp(16);

///安卓系统自带的[FontWeight.bold]在个别手机上会显示的很粗，使用一个轻量的粗字
FontWeight get lightBold => SafePlatform.isAndroid() ? FontWeight.w600 : FontWeight.w700;

extension ContextExtension on BuildContext? {

  /// 退出当前页面,建议都使用这个方法
  void pop<T extends Object?>([T? result]) {
    final BuildContext? context = this;
    if (context == null) {
      return;
    }
    if (Navigator.canPop(context)) {
      Navigator.pop(context, result);
    } else {
      SystemNavigator.pop();
    }
  }

  ///[duration] 是否延迟打开页面，单位是毫秒
  Future push(Widget page, {int? duration}) {
    final BuildContext? context = this;
    if (context == null) {
      return Future.value();
    }
    if(duration == null) {
      return Navigator.push(context, SlidePageRoute(page));
    } else {
      return Future.delayed(Duration(milliseconds: duration)).then((_) {
        return Navigator.push(context, SlidePageRoute(page));
      });
    }
  }

  ///[duration] 是否延迟打开页面，单位是毫秒
  Future pushReplacement(Widget page, {dynamic result, int? duration}) {
    final BuildContext? context = this;
    if (context == null) {
      return Future.value();
    }
    if(duration == null) {
      return Navigator.pushReplacement(context, SlidePageRoute(page), result: result);
    } else {
      return Future.delayed(Duration(milliseconds: duration)).then((_) =>
          Navigator.pushReplacement(context, SlidePageRoute(page), result: result));
    }
  }
}

bMaterialPageRoute(Widget widget, {RouteSettings? settings}) {
  RouteSettings newSettings;
  if (settings == null) {
    newSettings = RouteSettings(name: widget.runtimeType.toString());
  } else if (settings.name.isNullOrEmpty) {
    newSettings = RouteSettings(
        name: widget.runtimeType.toString(), arguments: settings.arguments);
  } else {
    newSettings = settings;
  }
  return MaterialPageRoute(
      builder: (BuildContext context) => widget, settings: newSettings);
}

///自定义页面切换效果
abstract class WidgetRouteBuilder extends PageRouteBuilder {
  final Widget widget;

  WidgetRouteBuilder(this.widget, RouteSettings? settings, RouteTransitionsBuilder transition,
      {Duration duration = const Duration(milliseconds: 500),
      bool opaque = true})
      : super(
            opaque: opaque,
            settings: settings,
            pageBuilder:
                (BuildContext c, Animation<double> a1, Animation<double> a2) {
              return widget;
            },
            transitionDuration: duration,
            transitionsBuilder: transition);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    child = super.buildTransitions(context, animation, secondaryAnimation, child);
    if(SafePlatform.isAndroid()) return child;
    //下面这2行代码让IOS可支持侧滑返回，但安卓上运行这2行代码会显得卡顿，就不执行了，直接返回
    PageTransitionsTheme theme = Theme.of(context).pageTransitionsTheme;
    return theme.buildTransitions(this, context, animation, secondaryAnimation, child);
  }

  String widgetName() {
    return widget.runtimeType.toString();
  }
}

///从右向左的切换效果
class SlidePageRoute extends WidgetRouteBuilder {

  SlidePageRoute(Widget widget, {bool opaque = true, RouteSettings? settings}) : super(
      widget, settings, (BuildContext c,
      Animation<double> a1, Animation<double> a2, Widget child) {
    //左右滑动
    return SlideTransition(
      position: Tween<Offset>(
          begin: const Offset(1.0, 0.0), end: const Offset(0.0, 0.0))
          .animate(CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  }, opaque: opaque);
}

///渐变出来效果
class FadePageRoute extends WidgetRouteBuilder {
  FadePageRoute(Widget widget, {bool opaque = true, RouteSettings? settings})
      : super(widget, settings, (BuildContext c, Animation<double> a1,
            Animation<double> a2, Widget child) {
          //渐变过渡
          return FadeTransition(
            //渐变过渡 0.0-1.0
            opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
              parent: a1, //动画样式
              curve: Curves.fastOutSlowIn, //动画曲线
            )),
            child: child,
          );
        }, opaque: opaque);
}

///旋转缩放效果
class RotationPageRoute extends WidgetRouteBuilder {
  RotationPageRoute(Widget widget, {RouteSettings? settings})
      : super(widget,settings, (BuildContext c, Animation<double> a1,
            Animation<double> a2, Widget child) {
          //翻转缩放
          return RotationTransition(
            turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
              parent: a1,
              curve: Curves.fastOutSlowIn,
            )),
            child: ScaleTransition(
              scale: Tween(begin: 0.0, end: 1.0).animate(
                  CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),
              child: child,
            ),
          );
        });
}

///底部弹出效果
class BottomPageRoute extends WidgetRouteBuilder {

  @override
  bool canTransitionTo(TransitionRoute<dynamic> nextRoute) => false;
  @override
  bool canTransitionFrom(TransitionRoute<dynamic> previousRoute) => false;
  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return transitionsBuilder(context, animation, secondaryAnimation, child);
  }

  BottomPageRoute(Widget widget, {RouteSettings? settings}) : super(
      widget, settings, (BuildContext c,
      Animation<double> a1, Animation<double> a2, Widget child) {

    //上下滑动
    return SlideTransition(
      position: Tween<Offset>(
          begin: const Offset(0.0, 1.0), end: const Offset(0.0, 0.0))
          .animate(CurvedAnimation(parent: a1, curve: Curves.fastOutSlowIn)),
      child: child,
    );
  }, opaque: false);

}